package com.agent.generator.service;

import com.agent.common.exception.BusinessException;
import com.agent.common.result.ResultCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.agent.common.utils.IdGenerator;
import com.agent.generator.entity.GeneratedContent;
import com.agent.generator.mapper.GeneratedContentMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 生成内容服务
 * @author Jeff_Wan
 * @description 管理AI生成内容的存储与查询
 */
@Service
public class GeneratedContentService extends ServiceImpl<GeneratedContentMapper, GeneratedContent> {
    private final IdGenerator idGenerator = IdGenerator.getInstance();

    /**
     * 记录生成的内容
     * @param taskId 任务ID
     * @param contentPath 内容存储路径
     * @param contentType 内容类型
     * @param originalFilename 原始文件名
     */
    public void recordGeneratedContent(Long taskId, String contentPath, String contentType, String originalFilename) {
        GeneratedContent content = new GeneratedContent();
        content.setId(idGenerator.nextId());
        content.setTaskId(taskId);
        content.setContentPath(contentPath);
        content.setContentType(contentType);
        content.setOriginalFilename(originalFilename);
        save(content);
    }

    /**
     * 根据内容ID获取内容信息
     * @param contentId 内容ID
     * @return 内容信息
     */
    public GeneratedContent getContentInfo(Long contentId) {
        GeneratedContent content = getById(contentId);
        if (content == null) {
            throw new BusinessException(ResultCode.ASSET_NOT_FOUND);
        }
        return content;
    }

    /**
     * 根据任务ID获取该任务的所有生成内容
     * @param taskId 任务ID
     * @return 内容列表
     */
    public List<GeneratedContent> getContentsByTaskId(Long taskId) {
        QueryWrapper<GeneratedContent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("task_id", taskId);
        return list(queryWrapper);
    }
}